package com.learning.platform.repository;

import com.learning.platform.entity.Certificate;
import com.learning.platform.enums.CertificateStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

@Repository
public interface CertificateRepository extends JpaRepository<Certificate, Long> {
    
    /**
     * 分页查询用户证书
     */
    Page<Certificate> findByUserIdOrderByIssueDateDesc(Long userId, Pageable pageable);
    
    /**
     * 分页查询用户证书（别名方法）
     */
    default Page<Certificate> findByUserId(Long userId, Pageable pageable) {
        return findByUserIdOrderByIssueDateDesc(userId, pageable);
    }
    
    /**
     * 查询用户的所有证书
     */
    List<Certificate> findByUserIdAndStatusOrderByIssueDateDesc(Long userId, CertificateStatus status);
    
    /**
     * 根据证书编号查询
     */
    Optional<Certificate> findByCertificateNumber(String certificateNumber);
    
    /**
     * 根据验证码查询
     */
    Optional<Certificate> findByVerificationCode(String verificationCode);
    
    /**
     * 验证证书（根据证书编号和验证码）
     */
    Optional<Certificate> findByCertificateNumberAndVerificationCode(String certificateNumber, String verificationCode);
    
    /**
     * 验证证书方法别名
     */
    default Optional<Certificate> validateCertificate(String certificateNumber, String verificationCode) {
        return findByCertificateNumberAndVerificationCode(certificateNumber, verificationCode);
    }
    
    /**
     * 查询用户的课程证书
     */
    Optional<Certificate> findByUserIdAndCourseId(Long userId, Long courseId);
    
    /**
     * 检查用户是否已有课程证书
     */
    boolean existsByUserIdAndCourseId(Long userId, Long courseId);
    
    /**
     * 统计用户证书数量
     */
    long countByUserIdAndStatus(Long userId, CertificateStatus status);
    
    /**
     * 统计课程证书数量
     */
    long countByCourseIdAndStatus(Long courseId, CertificateStatus status);
    
    /**
     * 按日期范围查询证书
     */
    @Query("SELECT c FROM Certificate c WHERE c.issueDate BETWEEN :startDate AND :endDate " +
           "AND (:status IS NULL OR c.status = :status) " +
           "ORDER BY c.issueDate DESC")
    Page<Certificate> findByIssueDateBetween(@Param("startDate") LocalDate startDate,
                                           @Param("endDate") LocalDate endDate,
                                           @Param("status") CertificateStatus status,
                                           Pageable pageable);
    
    /**
     * 查询课程的所有证书
     */
    Page<Certificate> findByCourseIdOrderByIssueDateDesc(Long courseId, Pageable pageable);
    
    /**
     * 统计每月颁发证书数量
     */
    @Query("SELECT YEAR(c.issueDate), MONTH(c.issueDate), COUNT(c) " +
           "FROM Certificate c WHERE c.status = 'ACTIVE' " +
           "GROUP BY YEAR(c.issueDate), MONTH(c.issueDate) " +
           "ORDER BY YEAR(c.issueDate) DESC, MONTH(c.issueDate) DESC")
    List<Object[]> countCertificatesByMonth();
}